//
//  BaseLoadingViewController.m
//  ChinaHongNiang
//
//  Created by Hancong on 15/6/30.
//  Copyright (c) 2015年 HC. All rights reserved.
//

#import "BaseLoadingViewController.h"
#import "RTSpinKitView.h"
#import "MBProgressHUD.h"
typedef void (^ActionHandle)(void);
@interface BaseLoadingViewController ()

@property (strong, nonatomic)MBProgressHUD *hud;
@property (strong, nonatomic)ActionHandle actionHandle;
@end

@implementation BaseLoadingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)addSpinViewWithView:(UIView *)view actionHandle:(void (^)(void))actionHandle
{
    //RTSpinKitViewStyleFadingCircleAlt  转圈
    //RTSpinKitViewStyleWave  波浪
    //RTSpinKitViewStyleThreeBounce  三个点波动
    //RTSpinKitViewStyleArcAlt   加载圆圈，速度过快
    RTSpinKitView *spinner = [[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStyleFadingCircleAlt color:[UIColor whiteColor]];
    
    self.hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    self.hud.animationType = MBProgressHUDAnimationFade;
    self.hud.square = YES;
    self.hud.mode = MBProgressHUDModeCustomView;
    self.hud.customView = spinner;
//    self.hud.color = [UIColor clearColor];
    
    self.hud.labelText = NSLocalizedString(@"Loading", @"Loading");
    //    hud.backgroundColor = [UIColor clearColor];
    [spinner startAnimating];
    if (actionHandle) {
        actionHandle();
    }
}

- (void)_removeSpinView
{
//    [self.spinView stopAnimating];
//    self.contentView.hidden = YES;
}

#pragma mark - Action
- (void)failedButtonTouch:(id)sender
{
    [self triggleLoadingAction];
}

- (void)triggleLoadingAction
{
    if (self.actionHandle) {
        [self setLoadingStatus:LoadingStatusAnimating];
        self.actionHandle();
    }
}

#pragma mark - Properties
- (void)setLoadingStatus:(LoadingStatus)loadingStatus
{
    switch (loadingStatus) {
        case LoadingStatusAnimating:
        {
//            [self.spinView startAnimating];
        }
            break;
        case LoadingStatusFinished:
        {
            //TODO:模拟2.0秒后加载完成
            [self.hud hide:YES afterDelay:2.0];
        }
            break;
        case LoadingStatusFailed:
        {
//            [self.spinView stopAnimating];
//            self.failedView.hidden = NO;
        }
            break;
        default:
            break;
    }
    _loadingStatus = loadingStatus;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
